package com.kang.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kang.entity.Folder;
import com.kang.entity.FolderCarousel;
import com.kang.entity.Notify;
import com.kang.mapper.FolderCarouselMapper;
import com.kang.mapper.FolderMapper;
import com.kang.service.FolderService;
import com.kang.vo.FolderVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;

@Service
public class FolderServiceImpl extends ServiceImpl<FolderMapper,Folder> implements FolderService {

    @Autowired
    private FolderMapper folderMapper;

    @Autowired
    private FolderCarouselMapper folderCarouselMapper;

    @Override
    public Integer insertFolder(Folder folder) {
        folder.setCreateTime(new Date());
        folder.setUpdateTime(new Date());
        folder.setIsShow(0);
        return folderMapper.insert(folder);
    }

    @Override
    public Integer deleteFolder(Folder folder) {
        //删除文件夹
        return folderMapper.deleteById(folder.getId());
    }

    @Override
    public Page<Folder> getFolderPage(FolderVo vo) {
        Page<Folder> page = new Page<>(vo.getPageNum(), vo.getPageSize());
        QueryWrapper<Folder> wrapper = new QueryWrapper<>();
        wrapper.eq(!StringUtils.isEmpty(vo.getFolderName()),"folder_name",vo.getFolderName());
        wrapper.between(!StringUtils.isEmpty(vo.getStartTime()),"create_time",vo.getStartTime(),vo.getEndTime());
        wrapper.last("order by create_time desc");
        return folderMapper.selectPage(page,wrapper);
    }

    @Override
    public Integer updateIsShow(FolderVo vo) {
        UpdateWrapper<Folder> wrapper = new UpdateWrapper<>();
        wrapper.eq("id",vo.getId());
        wrapper.set("is_show",vo.getIsShow() == 0 ? 1 : 0);
        return folderMapper.update(null,wrapper);
    }

    @Override
    public Page<Folder> getFolderShow() {
        Page<Folder> page = new Page<>(1, 5);
        QueryWrapper<Folder> wrapper = new QueryWrapper<>();
        wrapper.last("order by create_time desc");
        return folderMapper.selectPage(page,wrapper);
    }
}
